package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public List<User> hello(){

        return userService.findAll();
    }

    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        String token = userService.login(user);
        if(token==null||token.length()==0){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }

    @GetMapping("/list")
    public SysResult getUserlist(PageResult pageResult){//3
        pageResult = userService.getUserlist(pageResult);
        return SysResult.success(pageResult);//5
    }

    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }

    @PutMapping("/status/{id}/{status}")
    public SysResult status(User user){
        userService.status(user);
        return SysResult.success();
    }

    @DeleteMapping("/{id}")
    public SysResult deleteUser(@PathVariable Integer id){
            userService.deleteUser(id);
            return SysResult.success();

    }


    //    修改按钮（1、数据回显 2、修改数据）
    @GetMapping("/{id}")
    public SysResult getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return SysResult.success(user);
    }

    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }

    //商品管理



}
